package Web;

import Utils.JDBCUtils;

import javax.servlet.*;
import java.io.IOException;

/**
 * @author jianjun
 * @Describe
 * @creat 2022-01-04-16:40
 */
public class transactionFilter implements Filter {
    public void destroy() {
    }

    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
        try {
            chain.doFilter(req, resp);
            JDBCUtils.commitAndClose();//提交事务
        } catch (Exception e) {
            JDBCUtils.rollBackAndClose();//回滚事务
            e.printStackTrace();
            throw new RuntimeException(e);//将错误统一抛给Tomcat服务器，统一进行处理异常
        }
    }

    public void init(FilterConfig config) throws ServletException {

    }
}
